﻿using KouDai.Core.Engine;
using KouDai.Core.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace KouDai.Core.Parser
{
    public class HoneyParser : KouDaiParser
    {
        public override void ParseJson(string jsonData, CommunicationData communicationData)
        {
            try
            {
                if (!string.IsNullOrEmpty(jsonData) && communicationData != null)
                {
                    if (communicationData.BackData.ResposeData != null)
                    {
                        List<HoneyEntity> honeyList = communicationData.BackData.ResposeData as List<HoneyEntity>;
                        JArray jarray = JArray.Parse(jsonData);
                        JObject jobject = (JObject)jarray[1];
                        JToken jtoken = jobject.SelectToken("items");
                        foreach (JToken jt in jtoken.Children())
                        {
                            HoneyEntity honeyEntity = new HoneyEntity();
                            honeyEntity.ID = jt["id"].ToString();
                            honeyEntity.ImageUrlForIphone = jt["imageUrlForIphone"].ToString();
                            honeyEntity.Url = jt["imageUrlForIphone"].ToString();
                            honeyEntity.ImgRatio = jt["imgRatio"].ToString();
                            honeyEntity.Name = jt["name"].ToString();
                            honeyEntity.Price = jt["price"].ToString();
                            honeyEntity.Timestamp = Convert.ToInt64(jt["timestamp"].ToString());
                            honeyEntity.Height = 192;
                            honeyEntity.Width = 192 / Convert.ToDouble(honeyEntity.ImgRatio);
                            honeyList.Add(honeyEntity);
                        }
                    }
                }
                if (this._nextParser != null)
                {
                    this._nextParser.ParseJson(jsonData, communicationData);
                }
            }
            catch (System.Exception ex)
            {
                communicationData.BackData.IsSuccess = false;
                communicationData.BackData.ErrorMsg = "Error ocurred in " + this.GetType().ToString() + "Error Msg:" + ex.Message.ToString();
            }
           
        }
    }
}
